package com.wuba.rxjavademo.lyz.day0416;

import java.util.Random;
import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.Subscriber;
import rx.schedulers.Schedulers;

/**
 * Created by liyaxing on 2016/4/16.
 */
public class Demo01 {

    public static void main(String[] ss){

        Demo01 demo01 = new Demo01() ;
//        Observable<Integer> observable = demo01.createObservable01() ;
//        observable.subscribe(new Observer<Integer>() {
//            public void onCompleted() {
//                System.out.println("===onCompleted===") ;
//            }
//            public void onError(Throwable e) {
//                System.out.println("===onError===") ;
//            }
//            public void onNext(Integer integer) {
//                System.out.println("===onNext=== :" + integer) ;
//            }
//        }) ;
//        Observable<Long> observable = demo01.deferObservable() ;
//        for (int i = 0; i < 3; i++) {
//            observable.subscribe(l -> {
//                print(l);
//            });
//            try {
//                Thread.sleep(1000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }
        Observable<Long> observable = demo01.timeObservable() ;
        observable.subscribe(integer -> print("num : " + integer)) ;

        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    private Observable<Long> timeObservable(){
        return Observable.timer(5,TimeUnit.SECONDS) ;
    }

    private Observable<Integer> repeatObservable(){
        return Observable.just(1).repeat(10) ;
    }

    private Observable<Long> intervalObservable(){
        return Observable.interval(1, TimeUnit.SECONDS).subscribeOn(Schedulers.io()) ;
    }

    private Observable<Integer> fromObservable(){

        Integer[] array = {1,2,3,4,5,6} ;

        return Observable.from(array) ;
    }

    private Observable<Long> deferObservable(){
//        Observable<Long> observable = justObservable() ;
        return Observable.defer(() -> justObservable()) ;
    }

    private Observable<Long> justObservable(){
        print("------");
        return Observable.just(System.currentTimeMillis()) ;
    }

    private Observable<Integer> rangeObservable(){
        return Observable.range(10,10) ;
    }

    private Observable<Integer> createObservable01(){
        return Observable.create(new Observable.OnSubscribe<Integer>() {
            @Override
            public void call(Subscriber<? super Integer> subscriber) {
                for (int i = 0; i < 10; i++) {
                    int num = new Random().nextInt(10);
                    if (num > 6) {
                        subscriber.onError(new Exception("value > 6"));
                    } else {
                        subscriber.onNext(num);
                    }
                }
            }
        }) ;
    }

    private static void print(Object obj){
        System.out.println(obj) ;
    }

}
